package com.bykj.taketurns.findfragment.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bykj.taketurns.R;
import com.bykj.taketurns.activity.PlayVideoActivity;
import com.bykj.taketurns.findfragment.activity.SecondaryActivity;
import com.bykj.taketurns.findfragment.bean.FindBean;

import java.util.List;

/**
 * Created by Administrator on 2017/8/14.
 */

public class FindRelaAdapter extends RecyclerView.Adapter<FindRelaAdapter.ViewHolder> {
    private Context context;
    private List<FindBean.TypeListBean> list;
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_NORMAL = 1;
    private static View mHeaderView;

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
    }

    public void addDatas(Context context, List<FindBean.TypeListBean> typeList) {
        this.context = context;
        this.list = typeList;
    }


    @Override
    public int getItemViewType(int position) {
        if(mHeaderView == null) return TYPE_NORMAL;
        if(position == 0) return TYPE_HEADER;
        return TYPE_NORMAL;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mHeaderView != null && viewType == TYPE_HEADER) return new ViewHolder(mHeaderView);
        View view = LayoutInflater.from(context).inflate(R.layout.find_recyclerview_item, parent, false);
        return new ViewHolder(view);
    }



    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        if(getItemViewType(position) == TYPE_HEADER) return;

        final int pos = getRealPosition(holder);
//        FindBean.TypeListBean typeListBean = list.get(pos);
        holder.tv_title_find.setText(list.get(pos).getTypename());
        holder.tv_explain_find.setText(list.get(pos).getTypes());
        holder.tv_sum_find.setText(list.get(pos).getStoryVideoSet().size() + "");
        holder.rv_data_find.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL));
        FindItemAdapter findItemAdapter = new FindItemAdapter(context,list.get(pos).getStoryVideoSet());
        holder.rv_data_find.setAdapter(findItemAdapter);

        findItemAdapter.setOnItemClickListener(new FindItemAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //跳转视频播放页面
                Intent intent=new Intent(context, PlayVideoActivity.class);
                context.startActivity(intent);
            }
        });

        holder.iv_more_find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(context, SecondaryActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("typeid", list.get(pos).getTypeid() + "");
                bundle.putString("title", list.get(pos).getTypename());
                intent.putExtra("bundle",bundle);
                context.startActivity(intent);

            }
        });

    }
    private int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }

    @Override
    public int getItemCount() {
        return mHeaderView == null ? list.size() : list.size() + 1;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{

        TextView tv_signboard_find;
        TextView tv_title_find;
        TextView tv_explain_find;
        TextView tv_sum_find;
        RecyclerView rv_data_find;
        ImageView iv_more_find;

        public ViewHolder(View view) {
            super(view);

            if(itemView == mHeaderView) return;

            tv_signboard_find = view.findViewById(R.id.tv_signboard_find);
            tv_title_find = view.findViewById(R.id.tv_title_find);
            tv_explain_find = view.findViewById(R.id.tv_explain_find);
            tv_sum_find = view.findViewById(R.id.tv_sum_find);
            rv_data_find = view.findViewById(R.id.rv_data_find);
            iv_more_find = view.findViewById(R.id.iv_more_find);
        }
    }
}
